package com.wms.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wms.context.BaseContext;
import com.wms.dto.GoodsInPageQueryDTO;
import com.wms.dto.GoodsOutPageQueryDTO;
import com.wms.entity.*;
import com.wms.exception.GoodsOutException;
import com.wms.mapper.GoodsMapper;
import com.wms.mapper.GoodsOutMapper;
import com.wms.mapper.UserMapper;
import com.wms.mapper.UserRoleMapper;
import com.wms.result.PageResult;
import com.wms.service.GoodsOutService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class GoodsOutServiceImpl implements GoodsOutService {
    @Autowired
    private GoodsOutMapper goodsOutMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 分页查询
     * @param goodsOutPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(GoodsOutPageQueryDTO goodsOutPageQueryDTO) {
        log.info("分页查询仓库信息:{}", goodsOutPageQueryDTO);
        //判断当前用户的角色
        WmsUser user = WmsUser.builder().userId(BaseContext.getCurrentId()).build();
        List<WmsUserRole> roleIdByUser = userRoleMapper.findRoleIdByUser(user);
        if (roleIdByUser.get(0).getRoleId() == 4) {
            log.info("当前为员工");
            goodsOutPageQueryDTO.setUserName(userMapper.getById(BaseContext.getCurrentId()));
        }
        PageHelper.startPage(goodsOutPageQueryDTO.getPage(), goodsOutPageQueryDTO.getPageSize());

        Page<WmsGoodsout> page = goodsOutMapper.pageQuery(goodsOutPageQueryDTO);
        long total = page.getTotal();
        List<WmsGoodsout> result = page.getResult();
        return new PageResult(total, result);
    }

    /**
     * 批量删除仓库
     * @param ids
     */
    @Override
    public void delete(List<Integer> ids) {
        log.info("删除仓库:{}", ids);
        goodsOutMapper.delete(ids);
    }

    /**
     * 申请入库
     * @param wmsGoodsout
     */
    @Override
    public void applyIn(WmsGoodsout wmsGoodsout) {
        log.info("申请入库:{}", wmsGoodsout);
        // 设置申请为审核中
        wmsGoodsout.setStatus(0);
        // 设置申请时间为当前时间
        wmsGoodsout.setOutTime(LocalDateTime.now());
        // 设置申请人为当前登录的账号
        String username = userMapper.getById(BaseContext.getCurrentId());
        log.info("当前登录账号:{}", username);
        wmsGoodsout.setUserName(username);
        goodsOutMapper.save(wmsGoodsout);
    }

    /**
     * 审核通过
     * @param id
     */
    @Override
    @Transactional
    public void applyPass(int id) {
        //根据id查当前一行的数据
        WmsGoodsout wmsGoodsout = goodsOutMapper.getById(id);
        //设置状态为通过
        wmsGoodsout.setStatus(1);
        //根据物品名字查询物品
        WmsGoods wmsGoods = goodsMapper.getByName(wmsGoodsout.getGoodsName());
        //判断当前数量是否大于等于出库数量
        if (wmsGoods.getCount() < wmsGoodsout.getCount()) {
            throw new GoodsOutException("当前库存不足,无法出库");
        }
        //设置物品数量
        wmsGoods.setCount(wmsGoods.getCount() - wmsGoodsout.getCount());
        //设置出库时间
        wmsGoods.setInTime(LocalDateTime.now());
        //更新物品
        goodsMapper.update(wmsGoods);
        //更新入库信息
        goodsOutMapper.update(wmsGoodsout);
    }

    /**
     * 审核不通过
     * @param id
     */
    @Override
    @Transactional
    public void applyFail(int id) {
        //根据id查当前一行的数据
        WmsGoodsout wmsGoodsout = goodsOutMapper.getById(id);
        //设置状态为通过
        wmsGoodsout.setStatus(2);
        //更新入库信息
        goodsOutMapper.update(wmsGoodsout);
    }
}
